<!--
 * @Author: Bennty
 * @Date: 2024-08-27 11:16:17
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2024-10-14 17:23:09
 * @Description: 请填写简介
-->
<template>
    <el-dialog v-model="props.visible" title="入库" @close="handleClose" width="20%" destroy-on-close>
        <el-form :model="formData" ref="formRef" label-width="auto">
            <el-form-item label="材料">
                <span>{{ props.data.productCategoriesName }}</span>
            </el-form-item>
            <el-form-item label="类别">
                <span>{{ props.data.productSubcategoryName }}</span>
            </el-form-item>
            <el-form-item label="规格">
                <span>{{ props.data.specsDetail }}</span>
            </el-form-item>
            <el-form-item label="当前库存">
                <span>{{ `${props.data.currentInventory}${props.data.unit}` }}</span>
            </el-form-item>
            <el-form-item label="入库日期" prop="inDate">
                <el-date-picker v-model="formData.inDate" placeholder="年/月/日" format="YYYY/MM/DD" value-format="YYYY-MM-DD"></el-date-picker>
            </el-form-item>
            <el-form-item label="入库数量" prop="number">
                <el-row>
                    <el-col :span="24">
                        <el-input type="number" v-model="formData.number" placeholder="请输入">
                            <template #append>
                                {{ props.data.unit }}
                            </template>
                        </el-input>
                    </el-col>
                </el-row>
            </el-form-item>
            <el-form-item label="关联采购单" prop="purchaseOrderId">
                <el-select v-model="formData.purchaseOrderId" placeholder="请选择">
                    <el-option v-for="item in purchaseList" :key="item.id" :label="`${item.arriveDate} 采购单`" :value="item.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
                <el-input v-model="formData.remark" type="textarea" maxlength="125" show-word-limit></el-input>
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button  type="primary" @click="handleSubmit">确 定</el-button>
                <el-button @click="handleClose">取 消</el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type { FormInstance } from 'element-plus';
import { getAuxiliaryPurchaseByIdApi, auxiliaryInStorageApi } from '@/api/sj/warehouse/auxiliary';

const props = defineProps(['visible', 'data']);
const emits = defineEmits(['close','submit']);
const purchaseList = ref<any[]>([]);
const formRef = ref<FormInstance>();
const formData = ref({
    date: undefined,
    inDate: undefined,
    number: undefined,
    purchaseOrderId: undefined,
    remark: undefined
});

const getAuxiliaryPurchase = async (id: any) => {
    const { data } = await getAuxiliaryPurchaseByIdApi(id);
    return data;
};

watch(()=>[props.data.id,props.visible], ([id, visible])=>{
    if(visible){
        getAuxiliaryPurchase(id).then(result=>{
            purchaseList.value = result;
        });
    };
},{deep: true});

const handleSubmit = async () => {
    const submitData = {
        ...formData.value,
        auxiliaryId: props.data.id,
        productCategoriesType: props.data.productCategoriesType,
        productCategoriesName: props.data.productCategoriesName,
        productSubcategoryType: props.data.productSubcategoryType,
        productSubcategoryName: props.data.productSubcategoryName,
        specifications: props.data.specsDetail
    };
    const { code } = await auxiliaryInStorageApi(submitData);
    if(code == 200){
        emits('submit', true);
    }else{
        emits('submit',false);
    };
};

const handleClose = ()=>{
    emits('close');
    formRef.value?.resetFields();
};
</script>

<style lang="scss" scoped>
.dialog-footer{
    display: flex;
    justify-content: center;
}
</style>